package com.zrx.springboot.web;

import com.zrx.springboot.entity.User;
import com.zrx.springboot.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 用户控制器
 */
@Controller
@RequestMapping("/user")
public class UerController {
    @Autowired
    private UserService userService;
    /**
     * 用户名是否存在
     */
    @ResponseBody
    @RequestMapping("/checkUserName")
    public  String checkUserName(String username){

        return userService.checkUser(username);
    }

    /**用户注册
     * @param user
     * @return
     */
    @ResponseBody
    @RequestMapping("/register")
    public String register(User user){
        System.out.println(user);
        userService.save(user);
        return "success";
    }

    /**
     * 登入
     * @param user
     * @param session
     * @return
     */
    @ResponseBody
    @RequestMapping("/login")
    public String login(User user, HttpSession session,String vehicle, HttpServletResponse response, HttpServletRequest request ){
        System.out.println("-------------");
        System.out.println(vehicle);
        System.out.println("-------------");
        return userService.loginCheck(user,session,vehicle,response,request);
    }
    /**
     * 安全退出
     */
    @RequestMapping("/logout")
    public String logout(HttpSession session){
        session.invalidate();
        return "redirect:/book/index";
    }
}
